package com.akk.module.admin.service;

import com.akk.module.admin.entity.*;
import com.akk.module.admin.mapper.AdminMenuMapper;
import com.akk.module.admin.mapper.AdminRoleMenuMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AdminMenuService {

    @Autowired
    AdminMenuMapper adminMenuMapper;
    @Autowired
    AdminMenuExample adminMenuExample;
    @Autowired
    AdminRoleMenuExample adminRoleMenuExample;
    @Autowired
    AdminRoleMenuMapper adminRoleMenuMapper;

    public List<AdminMenu> findMenuByRoleId(String roleId) {
        adminRoleMenuExample.clear();
        adminMenuExample.clear();
        adminRoleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
        List<AdminRoleMenu> RoleMenus = adminRoleMenuMapper.selectByExample(adminRoleMenuExample);
        List<String> menuIds = new ArrayList<>();
        for(AdminRoleMenu roleMenu:RoleMenus){
            menuIds.add(roleMenu.getMenuId());
        }
        adminMenuExample.createCriteria().andMenuIdIn(menuIds);
        return adminMenuMapper.selectByExample(adminMenuExample);
    }
    List<AdminMenu> subMenus(AdminMenu menu, List<AdminMenu> list){
        List<AdminMenu> subMenu = new ArrayList<>();
        List<AdminMenu> cloneList = new ArrayList<>();
        cloneList.addAll(0,list);
        for (AdminMenu menu1:list){
            if("0".equals(menu1.getMenuId())){
                continue;
            }
            if (menu1.getMenuUpId().equals( menu.getMenuId())) {
                cloneList.remove(menu1);
                menu1.setList(subMenus(menu1,cloneList));
                subMenu.add(menu1);
            }
        }
        return subMenu;
    }

    public List<AdminMenu> getMenuTree(List<AdminMenu> menus) {
        AdminMenu rootMenu = null;
        for(AdminMenu menu: menus){
            if("0".equals(menu.getMenuId())){
                rootMenu = menu;
            }
        }
        List<AdminMenu>menuTree = subMenus(rootMenu,menus);
        return menuTree;

    }

    public Set<String> getAdminPermissions(List<AdminMenu> menus) {
        List<String> permsList = new ArrayList<>();
        for(AdminMenu menu : menus){
            permsList.add(menu.getMenuPerms());
        }
        Set<String> permsSet = new HashSet<>();
        for(String perms:permsList){
            if(StringUtils.isBlank(perms)){
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }

    public List<AdminMenu> findByParam(Map param) {
        adminMenuExample.clear();
        if(param !=null){
            if(param.containsKey("menuName")){
                adminMenuExample.createCriteria().andMenuNameLike(param.get("menuName").toString());
            }
            if(param.containsKey("adminType")){
                adminMenuExample.createCriteria().andMenuTypeEqualTo(Integer.valueOf(param.get("adminType").toString()));
            }
        }
        adminMenuExample.setOrderByClause("menu_name");
        return adminMenuMapper.selectByExample(adminMenuExample);
    }

    public void update(AdminMenu menu) {
        adminMenuMapper.updateByPrimaryKey(menu);
    }

    public void save(AdminMenu menu) {
        adminMenuMapper.insert(menu);
    }

    public Set<String> getAdminPermissions(Admin admin) {
        adminMenuExample.clear();
        List<AdminMenu> adminMenu;
        adminMenu = this.findMenuByRoleId(admin.getRoleId());
        return this.getAdminPermissions(adminMenu);

    }

    public List<AdminMenu> findAllMenu() {
        adminMenuExample.clear();
        return adminMenuMapper.selectByExample(adminMenuExample);
    }

    public AdminMenu findByMenuId(String menuId) {
        return adminMenuMapper.selectByPrimaryKey(menuId);
    }

    public List<AdminMenu> findByMenuIds(List<String> menuIds) {
        adminMenuExample.clear();
        adminMenuExample.createCriteria().andMenuIdIn(menuIds);
        return adminMenuMapper.selectByExample(adminMenuExample);
    }

    public void delete(String menuId) {
        adminMenuMapper.deleteByPrimaryKey(menuId);
    }

    public List<AdminMenu> findByMenuUpId(String menuUpId) {
        adminMenuExample.clear();
        adminMenuExample.createCriteria().andMenuUpIdEqualTo(menuUpId);
        return adminMenuMapper.selectByExample(adminMenuExample);
    }
}
